import com.ailk.kbi.kbirazor.component.NodeGroup;
import com.ailk.kbi.kbirazor.control.KbrControl;
import com.ailk.kbi.kbirazor.main.model.MainModel;
import com.ailk.kbi.kbirazor.model.AreaModel;
import com.ailk.kbi.kbirazor.model.BrandModel;
import com.ailk.kbi.kbirazor.model.KbrModel;
import com.ailk.kbi.kbirazor.model.TimeModel;
import com.ailk.kbi.kbirazor.view.AreaWindow;
import com.ailk.kbi.kbirazor.view.BrandWindow;
import com.ailk.kbi.kbirazor.view.CommunicationThresholdSet;
import com.ailk.kbi.kbirazor.view.DescTitleWindow;
import com.ailk.kbi.kbirazor.view.IncomeThresholdSet;
import com.ailk.kbi.kbirazor.view.ResizableTitleWindow;
import com.ailk.kbi.kbirazor.view.ResizableTitleWindowSkin;
import com.ailk.kbi.kbirazor.view.TimeWindow;
import com.ailk.kbi.kbirazor.view.TrafficThresholdSet;
import com.ailk.kbi.kbirazor.vo.KpiVO;
import com.ailk.kbi.kbirazor.vo.MindMapVO;
import com.ailk.kbi.kbirazor.vo.UserVO;

import flash.events.Event;
import flash.events.MouseEvent;
import flash.system.Security;

import mx.charts.Legend;
import mx.charts.LineChart;
import mx.collections.XMLListCollection;
import mx.controls.AdvancedDataGrid;
import mx.controls.Alert;
import mx.controls.advancedDataGridClasses.AdvancedDataGridColumn;
import mx.core.IFlexDisplayObject;
import mx.events.DragEvent;
import mx.managers.DragManager;
import mx.managers.PopUpManager;

import spark.components.TitleWindow;




[Bindable]
private var kbrModel:KbrModel = KbrModel.getInstance();

[Bindable]
private var areaModel:AreaModel = AreaModel.getInstance();

[Bindable]
private var timeModel:TimeModel = TimeModel.getInstance();

[Bindable]
private var brandModel:BrandModel = BrandModel.getInstance();

[Bindable]
private var mainModel:MainModel = MainModel.getInstance();


private var kbrControl:KbrControl = new KbrControl();
private function initApp():void
{
	Security.allowDomain("*");
	/**********权限模拟************/
	var userVO:UserVO = new UserVO();
	userVO.userId = "1";
	userVO.cityId = "999";
	userVO.areaId = "999";
	mainModel.userVO = userVO;
	
	/**********权限模拟************/
	
	
	kbrModel.addEventListener("anchorChange", anchorChangeHandler);
	
	/*脑图XML变更事件*/
	kbrModel.addEventListener("mindMapXMLChange", mindMapXMLChangeHandler);
	
	/*时间范围变更事件*/
	kbrModel.addEventListener("selectableRangeChange", selectableRangeChangeHandler);
	
	kbrModel.addEventListener("kpiValuesChange", kpiValuesChangeHandler);
	
	kbrModel.addEventListener("getKpiIdsComplete",getKpiIdsCompleteHandler);
	
	kbrControl.getMindMapList();
}




private var _mainNodeGroup:NodeGroup;

private function anchorChangeHandler(event:Event):void{
	if(_mainNodeGroup){
		_mainNodeGroup.removeEventListener("widthChanged", widthChangedHandler);
		_mainNodeGroup.removeEventListener("heightChanged", heightChangedHandler);
	}
	mainGroup.removeAllElements();
	_mainNodeGroup = new NodeGroup(XML(kbrModel.mindMapXML.node));
	mainGroup.addElement(_mainNodeGroup);	
	_mainNodeGroup.x = 10;
	_mainNodeGroup.y = 30;
	_mainNodeGroup.addEventListener("widthChanged", widthChangedHandler);
	_mainNodeGroup.addEventListener("heightChanged", heightChangedHandler);
}

private function widthChangedHandler(event:Event):void {
	mainGroup.width = _mainNodeGroup.width + 20;
}

private function heightChangedHandler(event:Event):void {
	mainGroup.height = _mainNodeGroup.height + 40;
	//	mainGroup.rotationY = -30;
	scrMain.verticalScrollBar.value = (mainGroup.height - scrMain.height)/2;
}

protected function hslider1_changeHandler(event:Event):void
{
	//	backgroundGradientColors="[#dddddd,#ffffff]"contentBackgroundColor
	//	mainGroup.setStyle("contentBackgroundColor",0xdddddd);
	mainGroup.z = hs.value;
	
}

/**
 * 处理脑图XML变更事件
 */
private function mindMapXMLChangeHandler(event:Event):void{
//	anchorChangeHandler(null);
	if(kbrModel.timeSQL == null){
		Alert.show("脑图未定义时间范围！");
	}
	else{
		kbrControl.getTimeRangeVO();
	}
}

private function kpiValuesChangeHandler(event:Event):void{
	//	Alert.show("kpiValuesChangeHandler");
	anchorChangeHandler(null);
}

/**日期选择范围变更事件*/
private function selectableRangeChangeHandler(event:Event):void{
	dfOpTime.selectableRange = kbrModel.selectableRange;
	dfOpTime.selectedDate = dfOpTime.selectableRange.rangeEnd;
//	opTimeChangeHandler();
	
//	var time:String;
	if(dfOpTime.selectedDate.month < 9 && dfOpTime.selectedDate.date <= 9) {
		kbrModel.currentDate = dfOpTime.selectedDate.fullYear +"-0" + (dfOpTime.selectedDate.month + 1) + "-0" +dfOpTime.selectedDate.date;
	} else if(dfOpTime.selectedDate.month < 9 && dfOpTime.selectedDate.date > 9){
		kbrModel.currentDate=dfOpTime.selectedDate.fullYear +"-0" + (dfOpTime.selectedDate.month + 1) + "-" +dfOpTime.selectedDate.date;
	} else if(dfOpTime.selectedDate.month >= 9 && dfOpTime.selectedDate.date > 9){
		kbrModel.currentDate=dfOpTime.selectedDate.fullYear +"-" + (dfOpTime.selectedDate.month + 1) + "-" +dfOpTime.selectedDate.date;
	} else if(dfOpTime.selectedDate.month >= 9 && dfOpTime.selectedDate.date <= 9){
		kbrModel.currentDate=dfOpTime.selectedDate.fullYear +"-" + (dfOpTime.selectedDate.month + 1) + "-0" +dfOpTime.selectedDate.date;
	}
	
//	kbrControl.getWarnKpiIds(time);
	kbrControl.getWarnKpiIds();
}

private function getKpiIdsCompleteHandler(event:Event):void{
//	kbrControl.getWarnKpiIds();
	
	for each(var kpiXML:XML in kbrModel.mindMapXML..kpi){
		var kpiId:String = kpiXML.@id;
		for each(var warnKpiId:String in kbrModel.warnKpiIds) {
			if(kpiId == warnKpiId) {
				kpiXML.@warn_flag = "1";
			}
		}
	}
	
//	Alert.show(kbrModel.mindMapXML.toString());
	opTimeChangeHandler();
}

private function opTimeChange():void{
	if(dfOpTime.selectedDate.month < 9 && dfOpTime.selectedDate.date <= 9) {
		kbrModel.currentDate = dfOpTime.selectedDate.fullYear +"-0" + (dfOpTime.selectedDate.month + 1) + "-0" +dfOpTime.selectedDate.date;
	} else if(dfOpTime.selectedDate.month < 9 && dfOpTime.selectedDate.date > 9){
		kbrModel.currentDate=dfOpTime.selectedDate.fullYear +"-0" + (dfOpTime.selectedDate.month + 1) + "-" +dfOpTime.selectedDate.date;
	} else if(dfOpTime.selectedDate.month >= 9 && dfOpTime.selectedDate.date > 9){
		kbrModel.currentDate=dfOpTime.selectedDate.fullYear +"-" + (dfOpTime.selectedDate.month + 1) + "-" +dfOpTime.selectedDate.date;
	} else if(dfOpTime.selectedDate.month >= 9 && dfOpTime.selectedDate.date <= 9){
		kbrModel.currentDate=dfOpTime.selectedDate.fullYear +"-" + (dfOpTime.selectedDate.month + 1) + "-0" +dfOpTime.selectedDate.date;
	}
	kbrControl.getWarnKpiIds();
}

private function opTimeChangeHandler():void{
	kbrControl.getKpiValues();
	//	dfOpTime.selectedDate;
}

private function changeMM(e:Event):void {
	if(e.currentTarget.selectedItem){
		kbrModel.mindMapVO = e.currentTarget.selectedItem as MindMapVO;
		kbrControl.loadMindMapXML();
	}
	
}

private function cmdAreaAnalyse_clickHandler(event:MouseEvent):void
{
	if(areaModel.windowFlag) {
		
	} else {
		var areaWindow:AreaWindow = PopUpManager.createPopUp(this, AreaWindow, false) as AreaWindow;
		PopUpManager.centerPopUp(areaWindow);
		
		areaModel.windowFlag = true;
	}
	
}
private function cmdTimeAnalyse_clickHandler(event:MouseEvent):void
{
	if(timeModel.windowFlag){
		
	} else {
		var timeWindow:TimeWindow = PopUpManager.createPopUp(this, TimeWindow, false) as TimeWindow;
		PopUpManager.centerPopUp(timeWindow);
		
		timeModel.windowFlag = true;
	}
	
}
private function cmdBrandAnalyse_clickHandler(event:MouseEvent):void
{
	if(brandModel.windowFlag){
		
	} else {
		var brandWindow:BrandWindow = PopUpManager.createPopUp(this, BrandWindow, false) as BrandWindow;
		PopUpManager.centerPopUp(brandWindow);
		
		brandModel.windowFlag = true;
	}
}
private function cmdThresholdSet_clickHandler(event:MouseEvent):void
{
	if(kbrModel.thresholdSetWindowFlag){
		
	} else {
		if(kbrModel.mindMapVO != null) {
			
			if(kbrModel.mindMapVO.title == '收入变化分析') {
				var incomeThresholdSet:IncomeThresholdSet = PopUpManager.createPopUp(this, IncomeThresholdSet, false) as IncomeThresholdSet;
				PopUpManager.centerPopUp(incomeThresholdSet);
				
				kbrModel.thresholdSetWindowFlag = true;
			} else if(kbrModel.mindMapVO.title == '话务量变化分析') {
				var trafficThresholdSet:TrafficThresholdSet = PopUpManager.createPopUp(this, TrafficThresholdSet, false) as TrafficThresholdSet;
				PopUpManager.centerPopUp(trafficThresholdSet);
				
				kbrModel.thresholdSetWindowFlag = true;
			} else if(kbrModel.mindMapVO.title == '月通讯用户变化分析') {
				var communicationThresholdSet:CommunicationThresholdSet = PopUpManager.createPopUp(this, CommunicationThresholdSet, false) as CommunicationThresholdSet;
				PopUpManager.centerPopUp(communicationThresholdSet);
				
				kbrModel.thresholdSetWindowFlag = true;
			}
		} else {
			Alert.show("请先选择脑图","提示");
		}
		
	}
}

protected function image1_clickHandler(event:MouseEvent):void
{
	if(!kbrModel.descTitleWindowFlag) {
		var descTitleWindow:DescTitleWindow = PopUpManager.createPopUp(this, DescTitleWindow, false) as DescTitleWindow;
		PopUpManager.centerPopUp(descTitleWindow);
	}
	
}
private function mainGroup_clickHandler(event:MouseEvent):void
{
	if(event.target==mainGroup && kbrModel.nodeSelectedFlag) {
		kbrModel.clickFocus();	
	} else {
		//					Alert.show("none");
	}
}